package spring.yongongsystem.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import spring.yongongsystem.POJO.dto.MerchantDTO;
import spring.yongongsystem.POJO.dto.MerchantPageQueryDTO;
import spring.yongongsystem.POJO.entity.Admin;
import spring.yongongsystem.POJO.entity.Merchant;
import spring.yongongsystem.exception.AccountLockedException;
import spring.yongongsystem.exception.AccountNotFoundException;
import spring.yongongsystem.exception.PasswordErrorException;
import spring.yongongsystem.mapper.MerchantMapper;
import spring.yongongsystem.result.PageResult;
import spring.yongongsystem.service.MerchantService;

import java.util.List;

@Service
public class MerchantServiceImpl implements MerchantService {

    @Autowired
    private MerchantMapper merchantMapper;

    @Override
    public void save(MerchantDTO merchantDTO) {
        Merchant merchants = new Merchant();

        //对象属性拷贝
        BeanUtils.copyProperties(merchantDTO,merchants);
        merchants.setPassword(DigestUtils.md5DigestAsHex(merchants.getPassword().getBytes()));
        merchantMapper.insert(merchants);
    }

    /**
     * 分页查询
     *
     * @param merchantPageQueryDTO
     * @return
     */
    public PageResult pageQuery(MerchantPageQueryDTO merchantPageQueryDTO) {
        // select * from employee limit 0,10
        //开始分页查询
        PageHelper.startPage(merchantPageQueryDTO.getPage(), merchantPageQueryDTO.getPageSize());

        Page<Merchant> page = merchantMapper.pageQuery(merchantPageQueryDTO);

        long total = merchantMapper.count();
        List<Merchant> records = page.getResult();

        return new PageResult(total, records);
    }

    @Override
    public void deleteById(Long id) {
        merchantMapper.deleteById(id);
    }

    @Override
    public Merchant login(MerchantDTO merchantDTO) {
        String username = merchantDTO.getUsername();
        String password = merchantDTO.getPassword();

        //1、根据用户名查询数据库中的数据
        Merchant merchant = merchantMapper.getByUsername(username);

        //2、处理各种异常情况（用户名不存在、密码不对、账号被锁定）
        if (merchant == null) {
            //账号不存在
            throw new AccountNotFoundException("帐号不存在");
        }

        //密码比对
        //对前端传过来的明文密码进行md5加密处理
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        if (!password.equals(merchant.getPassword())) {
            //密码错误
            throw new PasswordErrorException("密码错误");
        }
        //3、返回实体对象
        return merchant;
    }

    /**
     * 编辑商家
     * @param merchantDTO
     */
    @Override
    public void update(MerchantDTO merchantDTO) {
        Merchant merchant = new Merchant();
        BeanUtils.copyProperties(merchantDTO,merchant);
        merchant.setPassword(DigestUtils.md5DigestAsHex(merchant.getPassword().getBytes()));
        merchantMapper.update(merchant);
    }
}
